package com.riggie.extension.config;

import com.riggie.extension.constant.Constants;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DLConfig {

    @Bean("normalQueue")
    public Queue normalQueue(){
        return QueueBuilder.durable(Constants.NORMAL_QUEUE)
                .deadLetterExchange(Constants.DL_EXCHANGE) //正常队列绑定死信交换机
                .deadLetterRoutingKey("dlx")//设置发送给死信队列的RoutingKey
                //.ttl(10000) //过期时间
                //.maxLength(10) //队列最大长度
                .build();
    }

    @Bean("noramlExchange")
    public DirectExchange normalExchange(){
        return ExchangeBuilder.directExchange(Constants.NORAML_EXCHANGE).build();
    }

    @Bean("normalBingding")
    public Binding normalBinding(@Qualifier("noramlExchange") Exchange exchange, @Qualifier("normalQueue") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("normal").noargs();
    }

    //死信交换机和死信队列
    @Bean("dlQueue")
    public Queue dlQueue(){
       return QueueBuilder.durable(Constants.DL_QUEUE).build();
    }

    @Bean("dlExchange")
    public DirectExchange dlExchange(){
        return ExchangeBuilder.directExchange(Constants.DL_EXCHANGE).build();
    }

    @Bean("dlBinding")
    public Binding dlBinding(@Qualifier("dlExchange") Exchange exchange, @Qualifier("dlQueue")Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("dlx").noargs();
    }

}
